package org.example.线程;

public class 死锁代码 {

    static Object object1 = new Object();
    static Object object2 = new Object();

    public static void main(String[] args) {
        Runnable r1 = () ->{
          synchronized (object1){
              System.out.println(Thread.currentThread().getName() + ": get lock1");
              synchronized (object2){
                  System.out.println(Thread.currentThread().getName() + ": get lock2");
              }
          }
        };

        Runnable r2 = () ->{
            synchronized (object2){
                System.out.println(Thread.currentThread().getName() + ": get lock2");
                synchronized (object1){
                    System.out.println(Thread.currentThread().getName() + ": get lock1");
                }
            }
        };

        Thread t1 = new Thread(r1, "thread 1");
        Thread t2 = new Thread(r2, "thread 2");
        t1.start();
        t2.start();
    }
}
